Page({
  data: {
    activeTab: 'collected', // 当前激活的标签页
    collectedWords: [], // 收藏的单词
    wrongWords: [], // 错题本单词
    currentWords: [] // 当前显示的单词列表
  },

  onLoad: function(options) {
    this.loadWordsData();
  },

  onShow: function() {
    this.loadWordsData();
  },

  // 加载单词数据
  loadWordsData: function() {
    const collectedWords = wx.getStorageSync('wordCollections') || [];
    const wrongWords = wx.getStorageSync('wrongWords') || [];
    
    // 处理收藏的单词数据，确保格式统一
    const processedCollectedWords = collectedWords.map(item => {
      if (typeof item === 'string') {
        return {
          word: item,
          addedTime: '未知时间'
        };
      }
      return {
        word: item.word || item,
        meaning: item.meaning,
        pronunciation: item.pronunciation,
        addedTime: item.addedTime || '未知时间'
      };
    });

    // 处理错题本数据
    const processedWrongWords = wrongWords.map(item => {
      if (typeof item === 'string') {
        return {
          word: item,
          addedTime: '未知时间'
        };
      }
      return {
        word: item.word || item,
        meaning: item.meaning,
        pronunciation: item.pronunciation,
        addedTime: item.addedTime || '未知时间'
      };
    });

    this.setData({
      collectedWords: processedCollectedWords,
      wrongWords: processedWrongWords
    });

    // 根据当前标签更新显示的单词
    this.updateCurrentWords();
  },

  // 更新当前显示的单词列表
  updateCurrentWords: function() {
    const { activeTab, collectedWords, wrongWords } = this.data;
    let currentWords = [];
    
    if (activeTab === 'collected') {
      currentWords = collectedWords;
    } else if (activeTab === 'wrong') {
      currentWords = wrongWords;
    }
    
    this.setData({
      currentWords: currentWords
    });
  },

  // 切换标签
  switchTab: function(e) {
    const tab = e.currentTarget.dataset.tab;
    this.setData({
      activeTab: tab
    });
    this.updateCurrentWords();
  },

  // 跳转到单词详情页
  goToWordDetail: function(e) {
    const word = e.currentTarget.dataset.word;
    wx.navigateTo({
      url: `/pages/word-detail/word-detail?word=${word}`
    });
  },

  // 删除单词
  deleteWord: function(e) {
    const word = e.currentTarget.dataset.word;
    const type = e.currentTarget.dataset.type;
    
    wx.showModal({
      title: '确认删除',
      content: `确定要删除单词"${word}"吗？`,
      success: (res) => {
        if (res.confirm) {
          this.removeWordFromStorage(word, type);
        }
      }
    });
  },

  // 从存储中移除单词
  removeWordFromStorage: function(word, type) {
    let storageKey = '';
    if (type === 'collected') {
      storageKey = 'wordCollections';
    } else if (type === 'wrong') {
      storageKey = 'wrongWords';
    } else {
      return;
    }
    
    const words = wx.getStorageSync(storageKey) || [];
    const filteredWords = words.filter(item => {
      const wordText = typeof item === 'string' ? item : item.word;
      return wordText !== word;
    });
    
    wx.setStorageSync(storageKey, filteredWords);
    this.loadWordsData();
    
    wx.showToast({
      title: '删除成功',
      icon: 'success'
    });
  },

  // 清空当前标签的所有单词
  clearAll: function() {
    const { activeTab } = this.data;
    const tabName = activeTab === 'collected' ? '收藏' : '错题本';
    
    wx.showModal({
      title: '确认清空',
      content: `确定要清空所有${tabName}单词吗？此操作无法恢复。`,
      confirmColor: '#ff4444',
      success: (res) => {
        if (res.confirm) {
          this.clearAllWords();
        }
      }
    });
  },

  // 执行清空操作
  clearAllWords: function() {
    const { activeTab } = this.data;
    let storageKey = '';
    
    if (activeTab === 'collected') {
      storageKey = 'wordCollections';
    } else if (activeTab === 'wrong') {
      storageKey = 'wrongWords';
    } else {
      return;
    }
    
    wx.setStorageSync(storageKey, []);
    this.loadWordsData();
    
    wx.showToast({
      title: '清空成功',
      icon: 'success'
    });
  },

  // 重新学习错题（仅针对错题本）
  studyAgain: function() {
    const { wrongWords } = this.data;
    
    if (wrongWords.length === 0) {
      wx.showToast({
        title: '没有错题可学习',
        icon: 'none'
      });
      return;
    }
    
    wx.showModal({
      title: '重新学习',
      content: `开始重新学习${wrongWords.length}个错题单词？`,
      success: (res) => {
        if (res.confirm) {
          // 跳转到单词学习页面，并传递错题单词
          wx.navigateTo({
            url: '/pages/word-study/word-study?type=wrong'
          });
        }
      }
    });
  },

  // 返回上一页
  goBack: function() {
    wx.navigateBack();
  }
}); 